package com.moqianfan.mcuassistant.activity;

import android.content.Intent;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.adapter.CommDataAdapter;
import com.moqianfan.mcuassistant.bean.CCourse;
import com.moqianfan.mcuassistant.bean.McuSource;
import com.moqianfan.mcuassistant.database.CCourseDao;
import com.moqianfan.mcuassistant.database.McuSourceDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/6/15.
 * C语言教程 设计实例  常用芯片  电路基础
 */
public class SourceListActivity extends BaseActivity implements AdapterView.OnItemClickListener, TextWatcher {
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.list_source)
    ListView listSource;
    @BindView(R.id.edit_list_search)
    EditText editSearch;

    /**
     * C语言教程 参数
     */
    private CCourseDao courseDao;
    private CommDataAdapter<CCourse> cAdapter;
    private List<CCourse> cCourses;
    private List<CCourse> searchCc;

    /**
     * 单片机 参数
     */
    private McuSourceDao sourceDao;
    private CommDataAdapter<McuSource> mcuAdapter;
    private List<McuSource> mcuSources;
    private List<McuSource> searchMcu;

    private String title;

    @Override
    public void findViews() {
        setContentView(R.layout.activity_list);
    }

    @Override
    public void initViews() {
        cCourses = new ArrayList<>();
        mcuSources = new ArrayList<>();
        searchCc = new ArrayList<>();
        searchMcu = new ArrayList<>();
        courseDao = appConfig.getDaoSession().getCCourseDao();
        sourceDao = appConfig.getDaoSession().getMcuSourceDao();

        title = getIntent().getStringExtra("title");
        txtTitle.setText(title);
        listSource.setOnItemClickListener(this);
        editSearch.addTextChangedListener(this);
    }

    @Override
    public void initDatas() {
        if ("C语言基础".equals(title) || "程序设计".equals(title) || "经典算法".equals(title)) {
            QueryBuilder qb = courseDao.queryBuilder();
            switch (title) {
                case "C语言基础":
                    qb.where(CCourseDao.Properties.Head.eq("C语言教程"));
                    break;
                case "经典算法":
                    qb.where(CCourseDao.Properties.Head.eq("经典算法"));
                    break;
                default:
                    qb.where(CCourseDao.Properties.Head.eq("经典实例"));
                    break;
            }
            List list = qb.list();
            for (Object item : list)
                cCourses.add((CCourse) item);
            if (null == cAdapter) {
                cAdapter = new CommDataAdapter<CCourse>(cCourses, R.layout.list_group) {
                    @Override
                    public void bindView(ViewHolder holder, CCourse obj) {
                        holder.setText(R.id.list_content_item, obj.getTitle());
                    }
                };
                listSource.setAdapter(cAdapter);
            } else {
                cAdapter.setmData(cCourses);
            }
        } else {
            QueryBuilder qb = sourceDao.queryBuilder();
            qb.where(McuSourceDao.Properties.Head.eq(title));
            List list = qb.list();
            for (Object item : list) {
                mcuSources.add((McuSource) item);
            }
            if (null == mcuAdapter) {
                mcuAdapter = new CommDataAdapter<McuSource>(mcuSources, R.layout.list_group) {
                    @Override
                    public void bindView(ViewHolder holder, McuSource obj) {
                        holder.setText(R.id.list_content_item, obj.getTitle());
                    }
                };
                listSource.setAdapter(mcuAdapter);
            } else {
                mcuAdapter.setmData(mcuSources);
            }
        }
    }

    @OnClick(R.id.image_close)
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.image_close:
                finish();
                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if ("C语言基础".equals(title) || "程序设计".equals(title) || "经典算法".equals(title)) {
            Intent intent = new Intent(SourceListActivity.this, WebInfoActivity.class);
            if (!TextUtils.isEmpty(editSearch.getText().toString())) {
                intent.putExtra("title", searchCc.get(position).getTitle());
                intent.putExtra("html", searchCc.get(position).getHtml());
                intent.putExtra("head", searchCc.get(position).getHead());
                intent.putExtra("desc", searchCc.get(position).getHead());
            } else {
                intent.putExtra("title", cCourses.get(position).getTitle());
                intent.putExtra("html", cCourses.get(position).getHtml());
                intent.putExtra("head", cCourses.get(position).getHead());
                intent.putExtra("desc", cCourses.get(position).getHead());
            }
            startActivity(intent);
        } else {
            Intent intent = new Intent(SourceListActivity.this, WebInfoActivity.class);
            if (!TextUtils.isEmpty(editSearch.getText().toString())) {
                intent.putExtra("title", searchMcu.get(position).getTitle());
                if (!TextUtils.isEmpty(searchMcu.get(position).getHtml()))
                    intent.putExtra("html", searchMcu.get(position).getHtml());
                else
                    intent.putExtra("url", searchMcu.get(position).getUrl());
                intent.putExtra("head", searchMcu.get(position).getHead());
                intent.putExtra("desc", searchMcu.get(position).getHead());
            } else {
                intent.putExtra("title", mcuSources.get(position).getTitle());
                if (!TextUtils.isEmpty(mcuSources.get(position).getHtml()))
                    intent.putExtra("html", mcuSources.get(position).getHtml());
                else
                    intent.putExtra("url", mcuSources.get(position).getUrl());
                intent.putExtra("head", mcuSources.get(position).getHead());
                intent.putExtra("desc", mcuSources.get(position).getHead());
            }
            startActivity(intent);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if (!TextUtils.isEmpty(s.toString())) {
            if ("C语言基础".equals(title) || "程序设计".equals(title) || "经典算法".equals(title)) {
                searchCc.clear();
                for (CCourse item : cCourses) {
                    if (item.getTitle().contains(s.toString()))
                        searchCc.add(item);
                }
                if (null == cAdapter) {
                    cAdapter = new CommDataAdapter<CCourse>(searchCc, R.layout.list_group) {
                        @Override
                        public void bindView(ViewHolder holder, CCourse obj) {
                            holder.setText(R.id.list_content_item, obj.getTitle());
                        }
                    };
                    listSource.setAdapter(cAdapter);
                } else {
                    cAdapter.setmData(searchCc);
                }
            } else {
                searchMcu.clear();
                for (McuSource item : mcuSources)
                    if (item.getTitle().contains(s.toString()))
                        searchMcu.add(item);
                if (null == mcuAdapter) {
                    mcuAdapter = new CommDataAdapter<McuSource>(searchMcu, R.layout.list_group) {
                        @Override
                        public void bindView(ViewHolder holder, McuSource obj) {
                            holder.setText(R.id.list_content_item, obj.getTitle());
                        }
                    };
                    listSource.setAdapter(mcuAdapter);
                } else {
                    mcuAdapter.setmData(searchMcu);
                }
            }
        } else {
            if ("C语言基础".equals(title) || "程序设计".equals(title) || "经典算法".equals(title)) {
                if (null == cAdapter) {
                    cAdapter = new CommDataAdapter<CCourse>(cCourses, R.layout.list_group) {
                        @Override
                        public void bindView(ViewHolder holder, CCourse obj) {
                            holder.setText(R.id.list_content_item, obj.getTitle());
                        }
                    };
                    listSource.setAdapter(cAdapter);
                } else {
                    cAdapter.setmData(cCourses);
                }
            } else {
                if (null == mcuAdapter) {
                    mcuAdapter = new CommDataAdapter<McuSource>(mcuSources, R.layout.list_group) {
                        @Override
                        public void bindView(ViewHolder holder, McuSource obj) {
                            holder.setText(R.id.list_content_item, obj.getTitle());
                        }
                    };
                    listSource.setAdapter(mcuAdapter);
                } else {
                    mcuAdapter.setmData(mcuSources);
                }
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }
}
